// pages/release-recruitment/release-recruitment.js
import { Job } from '../../models/job'
import { Company } from '../../models/company'
import UploadImg from '../../util/uploadImg'
// import UploadImgs from '../../util/uploadImgs'
import { Shop } from '../../models/shop'
import {check} from '../../util/identityCheck'
import {Location} from "../../util/location";

const shop = new Shop()
const jobModel = new Job()
const companyModel = new Company()
const uploadImg = new UploadImg()
// const uploadImgs = new UploadImgs()
var that
var list = []
Page({
  data: {
    username: '7878',
    checkIndex: 0,
    category: [],
    show: false,
    imgs: [],
    isShowDialog: false,
    isShowDialogTip: false,
    multiArray: [['全国', '北京', '安徽', '福建', '甘肃', '广东', '广西', '贵州', '海南', '河北', '河南', '黑龙江', '湖北', '湖南', '吉林', '江苏', '江西', '辽宁', '内蒙古', '宁夏', '青海', '山东', '山西', '陕西', '上海', '四川', '天津', '西藏', '新疆', '云南', '浙江', '重庆', '香港', '澳门', '台湾'], ['全国']],
    multiIndex:[0,0],
    shopTransfer: {
      title: '',
      shrinkPic: '',
      transferArea: '',
      detailAddress: '',
      contact: '',
      phone: '',
      transferSectionId: '',
      transferModeId: '',
      floor: '',
      rent: '',
      transferMoney: '',
      envPic: '',
      floorHeight: '',
      acreageId: '',
      faceWidth: '',
      advantageInfo: '',
      oldRun: '',
      perCapita: ''
    },
    city: '',
    province: ''
  },

  onClick(event) {
    console.log(event)
    let {value, id} = event.target.dataset
    let data = `category[${this.data.checkIndex}].checked`
    let checkId = `category[${this.data.checkIndex}].checkId`
    console.log('------')
    this.setData({
      [data]: value,
      [checkId]: id
    })
  },

  finish () {
    this.hide()
  },

  show (event) {
    let index = event.currentTarget.dataset.index
    this.setData({
      checkIndex: index,
      show: true
    })
  },

  hide () {
    this.setData({
      show: false
    })
  },

  toggle(event) {
    const { cid } = event.currentTarget.dataset
    const checkbox = this.selectComponent(`.checkboxes-${cid}`)
    checkbox.toggle()
  },

  noop() {} ,

  chooseImg () {
    uploadImg.chooseImg(imgUrl => {
      let imgList = this.data.imgs
      let length = imgList.length
      imgList[length] = imgUrl
      this.setData({
        'shopTransfer.envPic': String(imgList),
        imgs: imgList
      })
    })
  },

  publish () {
    console.log(1)
    let shopTransfer = this.data.shopTransfer
    let category = this.data.category
    shopTransfer.transferSectionId = category[0].checkId
    shopTransfer.transferModeId = category[1].checkId
    shopTransfer.acreageId = category[2].checkId && String(category[2].checkId)
    shopTransfer.transferArea = this.data.province + this.data.city
    if (!shopTransfer.title) {
      wx.showToast({
        title: '请填写标题',
        icon: 'none'
      })
      return
    }
    if (!shopTransfer.transferSectionId) {
      wx.showToast({
        title: '请选择地段信息',
        icon: 'none'
      })
      return
    }
    if (!shopTransfer.acreageId) {
      wx.showToast({
        title: '请选择店铺面积',
        icon: 'none'
      })
      return
    }
    if (!shopTransfer.transferModeId) {
      wx.showToast({
        title: '请选择转让方式',
        icon: 'none'
      })
      return
    }
    if (!shopTransfer.transferMoney) {
      wx.showToast({
        title: '请选填写转让费',
        icon: 'none'
      })
      return
    }
    if (!shopTransfer.transferArea) {
      wx.showToast({
        title: '请选择所在城市',
        icon: 'none'
      })
      return
    }
    if (!shopTransfer.detailAddress) {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none'
      })
      return
    }
    if (!shopTransfer.contact) {
      wx.showToast({
        title: '请填写联系人',
        icon: 'none'
      })
      return
    }
    if (/^[0-9]*$/.test(shopTransfer.contact)) {
      wx.showToast({
        title: '联系人不可以是数字',
        icon: 'none'
      })
      return
    }
    if (!shopTransfer.phone) {
      wx.showToast({
        title: '请填写联系电话',
        icon: 'none'
      })
      return
    }
    if (!shopTransfer.shrinkPic) {
      wx.showToast({
        title: '请填上传封面',
        icon: 'none'
      })
      return
    }
    let imgs = this.data.imgs
    if (!imgs.length) {
      wx.showToast({
        title: '请填上传环境照片',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '发布中'
    })
    if (imgs[0].indexOf('http') >= 0) {
      shop.publish(shopTransfer, res => {
        this.showDialogTip()
        wx.hideLoading()
      }, err => {
        this.showDialog()
        wx.hideLoading()
      })
    } else {
      uploadImgs.upLoadImg(imgs, res => {
        this.setData({
          'shopTransfer.envPic': String(res),
          'imgs': res
        })
        shop.publish(shopTransfer, res => {
          this.showDialogTip()
          wx.hideLoading()
        }, err => {
          this.showDialog()
          wx.hideLoading()
        })
      })
    }
  },

  onReady () {
    that = this
    shop.category(res => {
      this.setData({
        category: res
      })
    })

    companyModel.info(res => {
      this.setData({
        'shopTransfer.companyId': res.id
      })
    })
  },

  chooseLocation () {
    let that = this
    wx.chooseLocation({
      success(res) {
        that.setData({
          'shopTransfer.detailAddress': res.address+res.name
        })
      }
    })
  },

  inputChange (evevt) {
    let key = `shopTransfer.${evevt.currentTarget.dataset.type}`
    this.setData({
      [key]: evevt.detail
    })
  },

  uploadCover () {
    uploadImg.chooseImg(imgUrl => {
      this.setData({
        'shopTransfer.shrinkPic': imgUrl
      })
    })
  },

  x (event) {
    let index = event.currentTarget.dataset.index
    let imgs = this.data.imgs
    imgs.splice(index, 1)
    this.setData({
      imgs
    })
  },

  chooseImgs () {
    uploadImgs.chooseImg(5 , res => {
      console.log(res)
      let imgs = this.data.imgs
      res.forEach(url => {
        imgs.push(url)
      })
      this.setData({
        imgs
      })
    })
  },

  showDialogTip () {
    this.setData({
      isShowDialogTip: true
    })
  },

  confirmDialogTip () {
    wx.navigateBack({
      delta: 1
    })
  },

  showDialog () {
    this.setData({
      isShowDialog: true
    })
  },

  confirmDialog (event) {
    wx.navigateTo({
      url: '/pages/corporate-certification-detail/corporate-certification-detail'
    })
  },


  onLoad (){
    const location = new Location()
    location.reverseGeocoder().then(res => {
      console.log(res)
      if(res.city){
        this.setData({
          province: res.province,
          city: res.city
        })
      }
    })
  },

  onShow () {
    if(!check())return
  }


})
